package calorie.com.calorie.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import calorie.com.calorie.R;
import calorie.com.calorie.adapter.ProfitRecodAdapter;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.ProfitRecordContact;
import calorie.com.calorie.response.ProfitRecordResponce;
import calorie.com.calorie.util.DownloadManagerUtil;
import calorie.com.calorie.view.DownLoadDialogView;
import retrofit2.Response;


/**
 * 收益记录
 */
public class ProfitRecordActivity extends MvpActivity<ProfitRecordContact.ProfitRecordPresent> implements ProfitRecordContact.IProfitRecordView {
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;

    List<ProfitRecordResponce.DataBean> mList;
    ProfitRecodAdapter mAdapter;
    private int pageNo = 1;
    AlertDialog alertDialog = null;


    private void showListDialog(ProfitRecordResponce.DataBean dataBean) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        String[] items = {"代币类型:" + dataBean.getTokenType(), "代币数量:" + dataBean.getTokenNum(), "奖励时间:" + sdf.format(new Date(dataBean.getTime())), dataBean.getRemarks(), "触发账号:" + dataBean.getTriggerAccount()};
        if (dataBean.getTriggerAccount() == null) {
            items[4] = "";
        }
        AlertDialog.Builder listDialog =
                new AlertDialog.Builder(ProfitRecordActivity.this);
        listDialog.setTitle("收益明细");
        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // which 下标从0开始
                // ...To-do
                /*Toast.makeText(ProfitRecordActivity.this,
                        "你点击了" + items[which],
                        Toast.LENGTH_SHORT).show();*/
            }
        });
        listDialog.show();
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        mList = new ArrayList<>();
        mAdapter = new ProfitRecodAdapter(context, R.layout.recy_item_profit_record, mList);


        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                showListDialog(mList.get(position));
            }
        });
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rv.setLayoutManager(manager);
        rv.setAdapter(mAdapter);

        mPresent.queryProfiRecord(context, pageNo);
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                mPresent.queryProfiRecord(context, pageNo);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNo++;
                mPresent.queryProfiRecord(context, pageNo);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_profit_record;
    }

    @Override
    public void getProfitRecordData(Response<ProfitRecordResponce> response) {
        refresh.finishRefresh();
        refresh.finishLoadMore();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            List<ProfitRecordResponce.DataBean> data = response.body().getData();
            if (pageNo == 1) {
                mList.clear();
            }
            mList.addAll(data);
            mAdapter.notifyDataSetChanged();
            if (!ObjectUtils.isEmpty(data) && data.size() >= 10) {
                refresh.setEnableLoadMore(true);
            } else {
                refresh.setEnableLoadMore(false);
            }
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected ProfitRecordContact.ProfitRecordPresent createPresent() {
        return new ProfitRecordContact.ProfitRecordPresent();
    }
}
